"""
takes index.html and makes web page suitable for pasting into blogger.
see http://asmallwhiteball.blogspot.co.uk/2013/01/wed-2-jan-2013-idea-for-shorthand.html for details.
"""

holecount=0
totalscore=0
ninescore=0
summary = {}

SHORTHAND={'F':'Fairway', 'f':'Fairway(Edge)', 'S':'Short', 'L':'Left', 'R':'Right', 'G':'Green', 'g':'Green(Edge)', 'O':'Over', 'B':'Bunker', 'P':'Penalty', 'X':'Lost Ball'}
DESCRIPTION={-5:'Joker', -4:'Dodo', -3: 'Albatros', -2: 'Eagle', -1:'Birdy', 0:'Par', 1:'Bogey', 2:'DoubleBogey', 3:'TrebleBogey'}

def tabular(data):
    """
    """
    print '<h1>Stats</h1>'
    print '<table border="1">'
    print '<tr><th>What</th><th>Count</th></tr>'
    for key in sorted(data):
        print '<tr><td>%s</td><td>%s</td></tr>' % (key, data[key])
    print '</table>'

def capture(what):
    """
    """
    global summary
    xwhat = what.strip().strip(',').strip('.')
    summary.setdefault(xwhat, 0)
    summary[xwhat] += 1
    return what

def expand(line):
    """
    """
    global holecount, totalscore, ninescore, summary

    bits = line.replace('<','').split('/')
    pre, abbr = bits[0].split('>')
    post = bits[1].replace('>','')
    assert pre==post
    par = int(pre.replace('par', ''))

    holecount += 1
    drive=''
    collect=''
    result=''
    score=0

    for ch in abbr:
        if not drive:
            if '0'<=ch<='9':
                collect+=ch
                continue
            elif collect:
                drive = collect+'Yds'
            else:
                drive='-'
            if ch in ('F', 'f'):
                capture('FIR')

        if ch in SHORTHAND:
            result += capture(SHORTHAND[ch]+', ')
            score += 1
        elif 'H'==ch:
            result += capture('In The Hole!')
            score += 1
            puts = 0
        elif 'M'==ch:
            result += capture('Mulligan, ')
        elif '1'<=ch<='9':
            result += capture(ch+' Put.')
            puts = int(ch)
            score += puts
        else:
            result += capture('Mulligan?['+ch+'], ')

    totalscore += score
    ninescore += score
    delta = score-par
    if score==1:
        description = 'Hole in 1'
    elif delta in DESCRIPTION:
        description = DESCRIPTION[delta]
    else:
        description = str(delta)+' Over'
    capture(description)

    if holecount==1:
        print '<table border="1">'
        print '<tr><th>Hole</th><th>Par</th><th>Shorthand</th><th>Drive</th><th>Summary</th><th>Score</th><th>What</th><th>Total</th></tr>'
    print '<tr><td>'+str(holecount)+'</td><td>'+str(par)+'</td><td>'+abbr+'</td><td>'+drive+'</td><td>'+result+'</td><td>'+str(score)+'</td><td>'+description+'</td><td>'+str(totalscore)+'</td></tr>'
    if holecount==9:
        print '<tr><td>==</td><td>==</td><td>=====</td>-<td>===</td><td>FRONT 9</td><td>==</td><td>==========</td><td>'+str(ninescore)+'</td></tr>'
        ninescore=0
    elif holecount==18:
        print '<tr><td>==</td><td>==</td><td>=====</td>-<td>===</td><td>BACK 9</td><td>==</td><td>==========</td><td>'+str(ninescore)+'</td></tr>'
        print '<tr><td>==</td><td>==</td><td>=====</td>-<td>===</td><td>TOTAL </td><td>==</td><td>==========</td><td>'+str(totalscore)+'</td></tr>'
        print '</table>'
        ninescore=0
        holecount=0
        totalscore=0
        tabular (summary)
        summary={}

for l in open('index.html'):
    l = l.strip()
    if '<h1>' in l:
        print l
    elif '<par' in l:
        expand (l)
